package com.neoCalf.neoGroup.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neoCalf.neoCommon.entity.ChatMessage;
import com.neoCalf.neoCommon.entity.ChatSession;
import com.neoCalf.neoCommon.entity.GroupInfo;
import com.neoCalf.neoCommon.enums.*;
import com.neoCalf.neoCommon.exception.BusinessException;
import com.neoCalf.neoCommon.model.dto.SendMessageDto;
import com.neoCalf.neoCommon.utils.DataTool;
import com.neoCalf.neoCommon.utils.SendUtils;
import com.neoCalf.neoCommon.utils.SessionSeq;
import com.neoCalf.neoGroup.feigns.ChatServiceFeign;
import com.neoCalf.neoGroup.mapper.GroupInfoMapper;
import com.neoCalf.neoGroup.service.SessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author 小牛
 * @version 1.0
 * @description TODO
 * @date 2025/4/18 19:37
 */
@Service("groupSessionService")
public class SessionServiceImpl extends ServiceImpl<GroupInfoMapper, GroupInfo> implements SessionService {
    @Autowired
    private SessionSeq sessionSeq;
    @Autowired
    private SendUtils sendUtils;
    @Autowired
    private ChatServiceFeign chatServiceFeign;

    @Override
    public ChatMessage sendText(SendMessageDto<Object> sendMessageDto) {
        UserContactTypeEnum contactTypeEnum = DataTool.getContactTypeEnum(sendMessageDto.getContactId());
        if (!contactTypeEnum.equals(UserContactTypeEnum.GROUP)) {
            throw new BusinessException(ResultCode.CODE_10017, "联系人类型错误");
        }
        GroupInfo groupInfo = getById(sendMessageDto.getContactId());
        if (groupInfo == null) {
            throw new BusinessException(ResultCode.CODE_10017, "群组不存在");
        }
        if (groupInfo.getStatus() == GroupStatusEnum.DIS_BAND.getType()) {
            throw new BusinessException(ResultCode.CODE_10017, "群组已解散");
        }
        if (groupInfo.getStatus() == GroupStatusEnum.SHUT_DOWN.getType()) {
            throw new BusinessException(ResultCode.CODE_10017, "群组封禁");
        }
        String sessionId = sendMessageDto.getSessionId();
        ChatSession chatSession = new ChatSession(sessionId, sendMessageDto.getLastMessage(), sendMessageDto.getSendTime());
        ChatMessage chatMessage = new ChatMessage(
                sessionSeq.next(sessionId),
                sessionId,
                MessageTypeEnum.CHAT.getType(),
                sendMessageDto.getMessageContent(),
                sendMessageDto.getSendUserId(),
                sendMessageDto.getSendUserNickName(),
                sendMessageDto.getSendTime(),
                sendMessageDto.getContactId(),
                contactTypeEnum.getType(),
                0
        );
        if (GroupStatusEnum.isAllowSend(groupInfo.getStatus())) {
            chatMessage.setStatus(MessageStatusEnum.FLUSH.getStatus());
            sendMessageDto.setMessageId(chatMessage.getMessageId());
            sendMessageDto.setMessageType(MessageTypeEnum.CHAT.getType());
            sendMessageDto.setStatus(chatMessage.getStatus());
            sendMessageDto.setContactType(UserContactTypeEnum.GROUP.getType());
            sendUtils.sendMessage(sendMessageDto);
        } else {
            chatMessage.setStatus(MessageStatusEnum.FAILED.getStatus());
            chatMessage.setFailReason(groupInfo.getStatus());
        }
        chatServiceFeign.updateChatSession(chatSession);
        chatServiceFeign.addChatMessage(chatMessage);
        return chatMessage;
    }

    @Override
    public ChatMessage sendMedia(SendMessageDto<MultipartFile> sendMessageDto) {
        return null;
    }
}
